package my.project.sn;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import android.view.View;

class DrawCanvasView extends View {
	
	Path path=new Path();
	Paint paint=new Paint();
	
	final int vertCenter=370; //vertical center of screen
	final int horizCenter= 240; //horizontal center of screen
	final int vertWidth=80;  //vertical width of sniff
	final int horizWidth= 460; //horizontal width of sniff
	final int deltaVertWidth=355; //begin Y coordinate of sniff
	final int deltaHorizWidth=10; //begin X coordinate of sniff
	
	public DrawCanvasView(Context context) { 
	    super(context); 
	    //setFocusable(true);  
	    
	}
	
	
	public void drawElement() {
		//Random rand=new Random();
		
		int x=0,y=0;
		
		x=generateX();
		y=generateY();
		
    	int deltX=elementSize(x,y);
    	int deltY=elementSize(x,y);
        path.moveTo(x, y);
        path.lineTo(x+deltX,y);
        path.lineTo(x+2*deltX,y+deltY);
        path.lineTo(x+2*deltX,y+2*deltY);
        path.lineTo(x+deltX,y+3*deltY);
        path.lineTo(x,y+3*deltY);
        path.lineTo(x-deltX,y+2*deltY);
        path.lineTo(x-deltX,y+deltY);
        path.lineTo(x, y);
        
        
       if (y>375)
        	paint.setColor(Color.RED);
        else
        	paint.setColor(Color.WHITE);
        
        
	}
	
	//generate coordinate X with random
	public int generateX() {
		int x=0;
		boolean flagPosition=false;
		Random rand=new Random();
		while (!flagPosition) {
			x=rand.nextInt(horizWidth)+deltaHorizWidth;
			if (Math.abs(x-horizCenter)<rand.nextDouble()*rand.nextInt((int) (horizWidth*0.9))) {
				flagPosition=true;
			}
		}
		return x;
	}
	
	//generate coordinate Y with random
	public int generateY() {
		int y=0;
		boolean flagPosition=false;
		Random rand=new Random();
		while (!flagPosition) {
			y=rand.nextInt(vertWidth)+deltaVertWidth;
			if (Math.abs(y-vertCenter)<rand.nextInt((int) (vertWidth*0.9))*rand.nextDouble()) 
				flagPosition=true;
		}
		return y;
			
	}
	
	//determine the size of the one element
	public int elementSize(int x, int y) {
		if (Math.abs(x-horizCenter)>horizCenter-6*deltaHorizWidth)
			return 1;
		if ((vertCenter+vertWidth/2-y)<vertWidth/9)
			return 1;
		if (Math.abs(x-horizCenter)>horizCenter-7*deltaHorizWidth)
			return 2;
		if ((vertCenter+vertWidth/2-y)<vertWidth/8)
			return 2;
		if ((vertCenter+vertWidth/2-y)<vertWidth/5)
			return 3;
		return 6;
		
	}
	
	
	
	
	@Override 
    protected void onDraw(Canvas canvas) { 
		
		for (int i=0; i<400; i++) {
			
			drawElement();		
			canvas.drawPath(path, paint);
		}
	
    }
}